package com.swn.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swn.common.util.DbHelper;
import com.swn.common.util.JSONUtil;
import com.swn.common.util.PageListData;
import com.swn.common.util.StringUtil;

@Service
@SuppressWarnings("all")
public class TestProService {

	@Autowired
	private DbHelper dbHelper;
	
	/**
	 * 查询所有数据;
	 * @return
	 * @throws Exception 
	 */
	public List<Map<String, Object>> queryProListAll() throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("select project_id,projectName,projectMassger,projectDate from test_pro");
		return dbHelper.queryForList(sb.toString(), null);
	}
	
	
	/**
	 * 添加数据;
	 * @param paraMap
	 * @return
	 */
	public int addPro(Map<String, Object> paraMap) {
		
		return dbHelper.saveMap(paraMap, "test_pro");
	}
	
	/**
	 * 删除数据;
	 * @param project_id
	 * @return
	 */
	public int deletePro(Integer project_id) {
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("project_id", project_id);
		return dbHelper.delete(map, "test_pro");
	}


	/**
	 * 编辑根据id获取到数据;
	 * @param project_id
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> queryProById(Integer project_id) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("select project_id,projectName,projectMassger,projectDate,bigSmall from test_pro where project_id="+project_id);
		return dbHelper.queryForMap(sb.toString());
	}


	/**
	 * 修改数据(保存);
	 * @param param
	 * @return
	 */
	public int updatePro(Map<String, Object> param) {
		return dbHelper.updateMap(param, "test_pro");
	}


	/**
	 * 查询所有数据(条件查询)分页;
	 * @param start
	 * @param length
	 * @param signupForm
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> queryTeacherList(Integer start, Integer length,
			String signupForm) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		PageListData pageListData = new PageListData();
		pageListData.setStart(start);
		pageListData.setLength(length);
		Map<String, Object> param = JSONUtil.getMapFromJsonString(signupForm);
		StringBuffer sb = new StringBuffer();
		sb.append("select project_id,projectName,projectMassger,projectDate,bigSmall from test_pro where 1=1");
		String name  = (String) param.get("projectName");
		String massger  = (String) param.get("projectMassger");
		if(StringUtil.isNotEmpty(name)){
			sb.append(" and projectName like '%"+name+"%'");
		}
		if(StringUtil.isNotEmpty(massger)){
			sb.append(" and projectMassger like '%"+massger+"%'"); 
		}
		List<Map<String,Object>> list = dbHelper.queryPageMapList(pageListData, sb.toString(), param);
		int total = pageListData.getCount();
		map.put("recordsTotal", total);                  // 总记录数
		map.put("recordsFiltered", total);  			 // 过滤后的总记录数 
		map.put("data", list); 
		return map; 
	}
}
